#!/usr/bin/perl
# 
# Exercise 11.2
# 
# Find all the Perl programs on your computer. 
# 
# Hint: Use File::Find. What do all Perl programs have in common?
#
# Answer to Exercise 11.2

use strict;
use warnings;
use File::Find;

# I know I asked for everything on the computer, but I'm just going to look in my
# home directory. 
find ( \&isperl, ($ENV{'HOME'}) );
#find ( \&isperl, '/') ); # (This would be everything on the computer.)

exit;

################################################################################
# Subroutines 
################################################################################

# Report all Perl files within File::Find
sub isperl {
	#
	# Here's a method that finds command interpretation lines
	#

	# Ignore files that aren't ASCII text files or aren't readable
	-T and -r or return 0;

	# open the file and see if the first line is a command interpreter line such as:
	#!/usr/bin/perl
	#
   	open(THISFILE, $_) or (print "$File::Find::name : cannot open\n") and return 0;

	my $firstline = <THISFILE>;
	close(THISFILE);
	$firstline or return 0;
	($firstline =~ /^#\!.*perl/) and (print $File::Find::name, "\n") and (return 1);
	return 0;
}

sub isperl2 {

	# Since you might not use command interpretation on your operating system, here's
	# a method that looks for the filename extension ".pl".
	#

	# Ignore files that aren't ASCII text files or aren't readable
	-T and -r or return 0;

	 /\.pl$/ and (print $File::Find::name, "\n") and (return 1);

	 return 0;
}

# Can you think of other ways to check?  Here's an idea: you can see if a Perl script is indeed 
# correct Perl syntax by the command "perl -c program_name".  Is an empty file a syntactically
# correct Perl program?  A file with just one number in it?
